﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.Network.Protocol.BASE.Factory
{
    // should NOT be done by the client.. only handled!!!
    public class FactoryQUI : ProtocolMessageFactory
    {
        public enum ParameterEnum : int
        {
            SessionID = 1,
            InitiatorSID = 2,
            TimeToReconnect = 3,
            Message = 4,
            RedirectURL = 5,
            TerminateTransfers = 6
        }

        public override ProtocolMessage Create(ProtocolMessage.MessageTypeEnum messageType, Dictionary<int, object> parameters)
        {
            ProtocolMessage proto = new ProtocolMessage(messageType, ProtocolFeatureBASE.FeatureActions.ActionQUI);
            // ensure the SessionId is first in the message
            proto.Parameters.Add(new ProtocolParameter(parameters[(int)ParameterEnum.SessionID].ToString()));

            // shouldnt we be stopping it from sending SessionId again? see what was done in FactorySTA
            foreach (ParameterEnum en in parameters.Keys)
            {
                if (en != ParameterEnum.SessionID)
                    proto.Parameters.Add(new ProtocolParameter(GetParameterCodeForParameter((ParameterEnum)en), parameters[(int)en].ToString()));
            }
            return proto;
        }

        private string GetParameterCodeForParameter(ParameterEnum param)
        {
            switch (param)
            {
                case ParameterEnum.SessionID:
                    return "SessionId";
                case ParameterEnum.InitiatorSID:
                    return "ID";
                case ParameterEnum.TimeToReconnect:
                    return "TL";
                case ParameterEnum.Message:
                    return "MS";
                case ParameterEnum.RedirectURL:
                    return "RD";
                case ParameterEnum.TerminateTransfers:
                    return "DI";
                default:
                    return "";
            }
        }
    }
}
